package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.UmsRoleResourceRelation;
import com.itheima.mapper.UmsRoleResourceRelationMapper;
import com.itheima.service.db.UmsRoleResourceRelationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UmsRoleResourceRelationServiceImpl implements UmsRoleResourceRelationService {
    @Autowired
    UmsRoleResourceRelationMapper urrrMapper;

    @Override
    public List<UmsRoleResourceRelation> findByRoleId(Long roleId) {
        LambdaQueryWrapper<UmsRoleResourceRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRoleResourceRelation::getRoleId, roleId);
        return urrrMapper.selectList(lqw);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<UmsRoleResourceRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRoleResourceRelation::getRoleId, roleId);
        urrrMapper.delete(lqw);
    }

    @Override
    public void add(Long[] resourceIds, Long roleId) {
        if (resourceIds != null) {
            for (Long resourceId : resourceIds) {
                UmsRoleResourceRelation urrr = new UmsRoleResourceRelation();
                urrr.setResourceId(resourceId);
                urrr.setRoleId(roleId);
                urrrMapper.insert(urrr);
            }
        }
    }
}
